package com.bstech.Utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Dates {


	
	//DATE
	public static long GetDate()
	{
		return DateToLong(new Date(System.currentTimeMillis()));
	}
	public static long GetDate(int year, int month, int day, int hour, int minute, int second)
	{
		return (year * 10000000000L) + (month * 100000000) + (day * 1000000) + (hour * 10000) + (minute * 100) + second;
	}
	private static long DateToLong(Date date)
	{
		return Long.parseLong((new SimpleDateFormat("yyyyMMddHHmmss")).format(date));
	}
	public static String DateToString(long date, boolean includeHour)
	{
		if(date == 0 ) 
			return includeHour ? "01/01/1900 00:00" : "01/01/1900";
		return new SimpleDateFormat(includeHour ? "dd/MM/yyyy HH:mm" : "dd/MM/yyyy").format(LongToDate(date));
	}
	public static String DateToDotNetString(long date)
	{
		//2000-08-17T16:32:32
		if(date == 0 ) return "1900-01-01T00:00:00";
		//return new SimpleDateFormat("yyyy-MM-dd").format(IntToDate(date)) + "T00:00:00";
		return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(LongToDate(date));
	}
	public static long DateFromDotNetString(String date)
	{
		return GetDate(Integer.parseInt(date.substring(0,4)),
					   Integer.parseInt(date.substring(5,7)),
					   Integer.parseInt(date.substring(8,10)),
					   Integer.parseInt(date.substring(11,13)),
					   Integer.parseInt(date.substring(14,16)),
					   Integer.parseInt(date.substring(17,19))
					   );
	}
	private static Date LongToDate(long date)
	{
		try {
			return (new SimpleDateFormat("yyyyMMddHHmmss")).parse(Long.toString(date));
		} catch (ParseException e) {
			return null;
		}
	}
	public static int GetYear(long date)
	{
		return Integer.parseInt(Long.toString(date).substring(0, 4));
	}
	public static int GetMonth(long date)
	{
		return Integer.parseInt(Long.toString(date).substring(4, 6));
	}
	public static int GetDay(long date)
	{
		return Integer.parseInt(Long.toString(date).substring(6, 8));
	}
	public static int GetHour(long date)
	{
		return Integer.parseInt(Long.toString(date).substring(8, 10));
	}
	public static int GetMinute(long date)
	{
		return Integer.parseInt(Long.toString(date).substring(10, 12));
	}
	public static int GetSecond(long date)
	{
		return Integer.parseInt(Long.toString(date).substring(12, 14));
	}
	public static String GetDescrizioneMese(int month)
	{
		switch(month)
		{
		case 1:
			return "Gennaio";
		case 2:
			return "Febbraio";
		case 3:
			return "Marzo";
		case 4:
			return "Aprile";
		case 5:
			return "Maggio";
		case 6:
			return "Giugno";
		case 7:
			return "Luglio";
		case 8:
			return "Agosto";
		case 9:
			return "Settembre";
		case 10:
			return "Ottobre";
		case 11:
			return "Novembre";
		case 12:
			return "Dicembre";
		default:
			return "???";
		}
	}
	
	public static String NumToOre(double ore)
	{
		if(ore == 0) return "0:00";
		
		 double h = Math.floor(ore);
		 double m = (ore % 1 * 100);
	     m = Math.round(m);
	     m = (m * 60 / 100) / 100;
	     
	     DecimalFormat df =  new DecimalFormat("#.00");
	     return df.format(h + m).replace('.', ':').replace(',', ':');
	}
	public static double OreToNum(String ore)
	{
		if(ore.equals("0:00")) return 0;
		double h = Double.parseDouble(ore.substring(0, ore.indexOf(':')));
		double m = Double.parseDouble(ore.substring(ore.indexOf(':')+1));
		if(m > 0)
		{
			m = (m * 100 / 60) / 100;
		}
		
		return (h + m);
	}
}
